View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v 1.3 2002/07/01 18:43:00 rdonkin Exp $ 3 * $Revision: 1.3 $ 4 * $Date: 2002/07/01 18:43:00 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: ElementDescriptor.java,v 1.3 2002/07/01 18:43:00 rdonkin Exp $ 61 */ 62 package org.apache.commons.betwixt; 63 64 import java.util.ArrayList; 65 import java.util.List; 66 67 import org.apache.commons.betwixt.expression.Expression; 68 69 /*** <p><code>ElementDescriptor</code> describes the XML elements 70 * to be created for a bean instance.</p> 71 * 72 * <p> It contains <code>AttributeDescriptor</code>'s for all it's attributes 73 * and <code>ElementDescriptor</code>'s for it's child elements. 74 * 75 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 76 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a> 77 * @version $Revision: 1.3 $ 78 */ 79 public class ElementDescriptor extends NodeDescriptor { 80 81 /*** 82 * Descriptors for attributes this element contains. 83 * Constructed lazily on demand from a List 84 */ 85 private AttributeDescriptor[] attributeDescriptors; 86 /*** 87 * Descriptors for child elements. 88 * Constructed lazily on demand from a List 89 */ 90 private ElementDescriptor[] elementDescriptors; 91 92 /*** 93 * The List used on construction. It will be GC'd 94 * after initilization and the array is lazily constructed 95 */ 96 private List attributeList; 97 98 /*** 99 * The List used on construction. It will be GC'd 100 * after initilization and the array is lazily constructed 101 */ 102 private List elementList; 103 104 /*** the expression used to evaluate the new context of this node 105 * or null if the same context is to be used */ 106 private Expression contextExpression; 107 108 /*** Whether this element refers to a primitive type (or property of a parent object) */ 109 private boolean primitiveType; 110 111 /*** 112 * Whether this collection element can be used 113 * as a collection element. Defaults to true 114 */ 115 private boolean wrapCollectionsInElement = true; 116 117 /*** Base constructor */ 118 public ElementDescriptor() { 119 } 120 121 public ElementDescriptor(boolean primitiveType) { 122 this.primitiveType = primitiveType; 123 } 124 125 /*** Creates a <code>ElementDescriptor</code> with no namespace URI or prefix */ 126 public ElementDescriptor(String localName) { 127 super( localName ); 128 } 129 130 public String toString() { 131 return 132 "ElementDescriptor[qname=" + getQualifiedName() + ",class=" + getPropertyType() 133 + ",singular=" + getSingularPropertyType() + "]"; 134 } 135 136 /*** Creates a <code>ElementDescriptor</code> with namespace URI and qualified name */ 137 public ElementDescriptor(String localName, String qualifiedName, String uri) { 138 super(localName, qualifiedName, uri); 139 } 140 141 /*** Returns true if this element has child elements */ 142 public boolean hasChildren() { 143 return elementDescriptors != null && elementDescriptors.length > 0; 144 } 145 146 /*** Returns true if this element has attributes */ 147 public boolean hasAttributes() { 148 return attributeDescriptors != null && attributeDescriptors.length > 0; 149 } 150 151 /*** Specifies if this is a collection element 152 * Normally only used with the WrapCollectionsInElement setting 153 * @param isCollection 154 */ 155 public void setWrapCollectionsInElement(boolean wrapCollectionsInElement) { 156 this.wrapCollectionsInElement = wrapCollectionsInElement; 157 } 158 159 /*** 160 * Returns if this element is a collection element 161 */ 162 public boolean isWrapCollectionsInElement() { 163 return this.wrapCollectionsInElement; 164 } 165 166 public void addAttributeDescriptor(AttributeDescriptor descriptor) { 167 if ( attributeList == null ) { 168 attributeList = new ArrayList(); 169 } 170 getAttributeList().add( descriptor ); 171 attributeDescriptors = null; 172 } 173 174 175 /*** Returns the attribute descriptors for this element */ 176 public AttributeDescriptor[] getAttributeDescriptors() { 177 if ( attributeDescriptors == null ) { 178 if ( attributeList == null ) { 179 attributeDescriptors = new AttributeDescriptor[0]; 180 } 181 else { 182 attributeDescriptors = new AttributeDescriptor[ attributeList.size() ]; 183 attributeList.toArray( attributeDescriptors ); 184 185 // allow GC of List when initialized 186 attributeList = null; 187 } 188 } 189 return attributeDescriptors; 190 } 191 192 /*** Set <code>AttributesDescriptors</code> for this element */ 193 public void setAttributeDescriptors(AttributeDescriptor[] attributeDescriptors) { 194 this.attributeDescriptors = attributeDescriptors; 195 this.attributeList = null; 196 } 197 198 public void addElementDescriptor(ElementDescriptor descriptor) { 199 if ( elementList == null ) { 200 elementList = new ArrayList(); 201 } 202 getElementList().add( descriptor ); 203 elementDescriptors = null; 204 } 205 206 /*** Returns the child element descriptors for this element */ 207 public ElementDescriptor[] getElementDescriptors() { 208 if ( elementDescriptors == null ) { 209 if ( elementList == null ) { 210 elementDescriptors = new ElementDescriptor[0]; 211 } 212 else { 213 elementDescriptors = new ElementDescriptor[ elementList.size() ]; 214 elementList.toArray( elementDescriptors ); 215 216 // allow GC of List when initialized 217 elementList = null; 218 } 219 } 220 return elementDescriptors; 221 } 222 223 /*** Set descriptors for child element of this element */ 224 public void setElementDescriptors(ElementDescriptor[] elementDescriptors) { 225 this.elementDescriptors = elementDescriptors; 226 this.elementList = null; 227 } 228 229 /*** Returns the expression used to evaluate the new context of this element */ 230 public Expression getContextExpression() { 231 return contextExpression; 232 } 233 234 /*** Sets the expression used to evaluate the new context of this element */ 235 public void setContextExpression(Expression contextExpression) { 236 this.contextExpression = contextExpression; 237 } 238 239 /*** @return whether this element refers to a primitive type (or property of a parent object) */ 240 public boolean isPrimitiveType() { 241 return primitiveType; 242 } 243 244 /*** Sets whether this element refers to a primitive type (or property of a parent object) */ 245 public void setPrimitiveType(boolean primitiveType) { 246 this.primitiveType = primitiveType; 247 } 248 249 // Implementation methods 250 //------------------------------------------------------------------------- 251 252 /*** 253 * Lazily creates the mutable List, nullifiying the array so that 254 * as items are added to the list the Array is ignored until it is 255 * explicitly asked for 256 */ 257 protected List getAttributeList() { 258 if ( attributeList == null ) { 259 if ( attributeDescriptors != null ) { 260 int size = attributeDescriptors.length; 261 attributeList = new ArrayList( size ); 262 for ( int i = 0; i < size; i++ ) { 263 attributeList.add( attributeDescriptors[i] ); 264 } 265 // force lazy recreation later 266 attributeDescriptors = null; 267 } 268 else { 269 attributeList = new ArrayList(); 270 } 271 } 272 return attributeList; 273 } 274 275 /*** Lazily creates the mutable List */ 276 protected List getElementList() { 277 if ( elementList == null ) { 278 if ( elementDescriptors != null ) { 279 int size = elementDescriptors.length; 280 elementList = new ArrayList( size ); 281 for ( int i = 0; i < size; i++ ) { 282 elementList.add( elementDescriptors[i] ); 283 } 284 // force lazy recreation later 285 elementDescriptors = null; 286 } 287 else { 288 elementList = new ArrayList(); 289 } 290 } 291 return elementList; 292 } 293 294 }

This page was automatically generated by Maven